<template>
  <div>
    <!--卡片-->
    <div class="elcard">
      <el-card>
        <!--查询-->
        <div class="search">
          <el-input
            v-model="input"
            class="search_input"
            placeholder="请输入要查询的4S店名"
          ></el-input>
          <el-button type="primary" @click="search">查 询</el-button>
        </div>
        <el-button
          class="addbtn"
          type="primary"
          @click="dialogFormVisible = true"
          >添加4S店</el-button
        >
      </el-card>
    </div>
    <!--添加的表单-->
    <div>
      <el-dialog title="添加4S店" :visible.sync="dialogFormVisible">
        <el-form :model="addform">
          <el-form-item label="4S店名称">
            <el-input v-model="addform.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="4S店简称">
            <el-input v-model="addform.sname" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="主修品牌">
            <el-input v-model="addform.cartype" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="折扣">
            <el-input v-model="addform.discount" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="所在区域">
            <el-input v-model="addform.area" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="addform.address" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addsstore">添 加</el-button>
        </div>
      </el-dialog>
    </div>
    <!--列表-->
    <div>
      <el-table :data="sstore.list" border style="width: 100%" :header-cell-style="headClass" :cell-style="rowClass">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column prop="sstoreId" label="编号">
        </el-table-column>
        <el-table-column prop="sstoreName" label="店铺名称">
        </el-table-column>
        <el-table-column prop="sstoreShortname" label="短名称">
        </el-table-column>
        <el-table-column prop="sstoreCartype" label="店铺主修类型">
        </el-table-column>
        <el-table-column prop="sstoreDiscount" label="店铺折扣">
        </el-table-column>
        <el-table-column prop="sstoreArea" label="区域">
        </el-table-column>
        <el-table-column prop="sstoreAddress" label="地址">
        </el-table-column>
        <el-table-column prop="action" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">修改</el-button>
            <el-button type="danger" @click="del(scope.row.sstoreId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--修改表单-->
    <div>
      <el-dialog title="修改品牌" :visible.sync="dialogFormEditVisible">
        <el-form :model="editform">
          <el-form-item label="编号 :">
            <el-input
              v-model="editform.id"
              auto-complete="off"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="4s店名称 :">
            <el-input v-model="editform.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="短名称 :">
            <el-input v-model="editform.sname" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="店铺主修类型 :">
            <el-input v-model="editform.cartype" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="店铺折扣 :">
            <el-input
              v-model="editform.discount"
              auto-complete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="区域 :">
            <el-input v-model="editform.area" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址 :">
            <el-input v-model="editform.address" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormEditVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveDept(editform.id)"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </div>
    <!--分页-->
    <div class="fenye">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="5"
        :total="sstore.total"
        @current-change="getcutpage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "SStore",
  data() {
    return {
      sstore: [],
      dialogFormVisible: false,
      dialogFormEditVisible: false,
      input: "",
      currenpage: "",
      page: 1,
      limit: 5,
      editform: {
        id: "",
        name: "",
        sname: "",
        cartype: "",
        discount: "",
        area: "",
        address: ""
      },
      addform: {
        name: "",
        sname: "",
        cartype: "",
        discount: "",
        area: "",
        address: ""
      }
    };
  },
  created() {
    this.getSSList(1, 5);
  },
  methods: {
    //获取数据渲染表格
    getSSList(page, limit) {
      this.$axios
        .post(
          this.$api.contentManege.sshop.page,
          JSON.stringify({
            page: page,
            limit: limit
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          console.log(response);
          var result = response.data;
          if (response.status === 200) {
            this.sstore = response.data.data;
          } else {
            this.$alert(result.message);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //分页
    getcutpage: function(val) {
      console.log(val);
      this.currenpage = val
      this.getSSList(val, 5);
    },
    //修改按钮渲染表单
    edit: function(obj) {
      console.log(obj);
      this.dialogFormEditVisible = true;
      this.editform.id = obj.sstoreId;
      this.editform.name = obj.sstoreName;
      this.editform.sname = obj.sstoreShortname;
      this.editform.cartype = obj.sstoreCartype;
      this.editform.discount = obj.sstoreDiscount;
      this.editform.area = obj.sstoreArea;
      this.editform.address = obj.sstoreAddress;
    },
    //修改表单的保存按钮
    saveDept: function(sstore_id) {
      console.log(sstore_id);
      console.log(this.editform.id);
      // 将修改的数据发给服务器，接收服务器的响应并处理
      this.$axios
        .post(
            this.$api.contentManege.sshop.updateSStore,
          JSON.stringify({
            sstoreId: sstore_id,
            sstoreName: this.editform.name,
            sstoreShortname: this.editform.sname,
            sstoreCartype: this.editform.cartype,
            sstoreDiscount: this.editform.discount,
            sstoreArea: this.editform.area,
            sstoreAddress: this.editform.address
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          var result = response.data;
          if (response.status === 200) {
            this.$alert("修改成功");
            this.getSSList(this.currenpage, 5);
            this.dialogFormEditVisible = false;
          } else {
            this.$alert(result.message);
          }
        })
        .catch(() => {
          this.$alert("请求出错");
        });
    },
    //添加表单添加按钮
    addsstore: function() {
      this.$axios
        .post(
          this.$api.contentManege.sshop.addSStore,
          JSON.stringify({
            sstoreName: this.addform.name,
            sstoreShortname: this.addform.sname,
            sstoreCartype: this.addform.cartype,
            sstoreDiscount: this.addform.discount,
            sstoreArea: this.addform.area,
            sstoreAddress: this.addform.address
          }),
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          console.log(response);
          var result = response.data;
          if (response.status === 200) {
            this.$alert("添加成功");
            this.getSSList(this.currenpage,5);
            this.dialogFormVisible = false;
          } else {
            this.$alert(result.message);
          }
        })
        .catch(() => {
          this.$alert("请求出错");
        });
    },
    //删除按钮
    del: function(sstoreId) {
      // 发起请求到服务器，然后删除数据
      this.$axios
        .post(
            this.$api.contentManege.sshop.deleteSStoreById,
          {
            sstoreId: sstoreId
          },
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          var result = response.data;
          if (response.status === 200) {
            this.$alert("删除成功");
            this.getSSList(this.currenpage, 5);
          } else {
            this.$alert(result.message);
          }
        })
        .catch(err => {
          console.log(err);
          this.$alert("请求出错");
        });
    },
    search: function() {
      this.$axios
        .post(
            this.$api.contentManege.sshop.selectLikeSStoreName,
          {
            sstoreName: this.input,
            page: this.page,
            limit: this.limit
          },
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(res => {
          if (res.data.code === "200") {
            this.sstore = res.data.data;
          } else {
            console.log(2);
          }
        })
        .catch(err => {
          console.log("err = ", err);
          this.$alert("请求出错");
        });
    },
    rowClass() { //表格数据居中显示
      return "text-align:center"
    },
    headClass() { //表头居中显示
      return "text-align:center"
    },
  }
};
</script>

<style scoped>
.cell {
  text-align: center;
}
.elcard {
  margin-bottom: 20px;
}
.fenye {
  margin-top: 20px;
}
.addbtn {
  float: right;
  margin: 20px;
}
.el-card__body {
  padding: 0 !important;
}
.search_input {
  width: 300px;
  margin: 20px 10px;
}
.search {
  float: left;
}
/deep/ .el-card__body {
  padding: 0;
}
</style>
